package mv.releaser.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NFO {
	private InputStreamReader	nfoISR;
	private PrintStream			nfoPS;
	private MvReleaser			mvReleaser;

	public NFO(MvReleaser mvReleaser, InputStreamReader nfoISR,
			PrintStream nfoPS) throws IOException {
		this.mvReleaser = mvReleaser;
		this.nfoISR = nfoISR;
		this.nfoPS = nfoPS;
	}

	public NFO(MvReleaser mvReleaser, InputStreamReader nfoISR) {
		this.mvReleaser = mvReleaser;
		this.nfoISR = nfoISR;
	}

	public String fillNFO() throws IOException {
		BufferedReader nfo = new BufferedReader(nfoISR);
		String line;
		StringBuffer result = new StringBuffer();

		do {
			line = nfo.readLine();
			if (line != null) {
				line = replaceSpecialField(line,
						"\\$RELEASE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getReleaseName(), "$RELEASE-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$ARTIST-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getArtist(), "$ARTIST-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$TITLE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getTitle(), "$TITLE-XXX:XXX$".length());

				line = replaceSpecialField(line,
						"\\$TYPE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser.getType(),
						"$TYPE-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$DATE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getReleaseDate(), "$TYPE-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$FORMAT-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getFormat(), "$FORMAT-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$SOURCE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getSource(), "$SOURCE-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$AUDIO-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getAudio(), "$AUDIO-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$DEINT-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getDeinterlace(), "$DEINT-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$YEAR-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser.getYear(),
						"$YEAR-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$GENRE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getGenre(), "$GENRE-XXX:XXX$".length());

				line = replaceSpecialField(line,
						"\\$SIZE-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser.getSize(),
						"$SIZE-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$CRC-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser.getCRC(),
						"$CRC-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$COMMENTS-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getComments(), "$CRC-XXX:XXX$".length());

				line = replaceSpecialField(line,
						"\\$RIPPER-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getRipper(), "$RIPPER-XXX:XXX$"
								.length());

				// details (length can be used instead minute+seconds, same for
				// res...
				line = replaceSpecialField(line,
						"\\$MINUTES-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getMinutes(), "$MINUTES-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$SECONDS-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getSeconds(), "$SECONDS-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$LENGTH-\\d\\d\\d:\\d\\d\\d\\$", mvReleaser
								.getLength(), "$SECONDS-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$WIDTH-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getWidth(), "$WIDTH-XXX:XXX$"
								.length());
				line = replaceSpecialField(line,
						"\\$HEIGHT-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getHeight(), "$HEIGHT-XXX:XXX$"
								.length());
				line = replaceSpecialField(line,
						"\\$RES-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getResolution(), "$RES-XXX:XXX$"
								.length());
				line = replaceSpecialField(line,
						"\\$FORMAT-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getFormat(), "$FORMAT-XXX:XXX$"
								.length());
				line = replaceSpecialField(line,
						"\\$FPS-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ mvReleaser.getFPS(), "$FPS-XXX:XXX$".length());

				// Options
				line = replaceSpecialField(line,
						"\\$HDTV-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isHdtv() ? 'X' : ' '),
						"$HDTV-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$DVDRIP-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isHdtv() ? 'X' : ' '),
						"$DVDRIP-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$READNFO-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isReadnfo() ? 'X' : ' '),
						"$READNFO-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$DIRFIX-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isDirfix() ? 'X' : ' '),
						"$DIRFIX-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$PROPER-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isProper() ? 'X' : ' '),
						"$PROPER-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$REPACK-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isRepack() ? 'X' : ' '),
						"$REPACK-XXX:XXX$".length());
				line = replaceSpecialField(line,
						"\\$NFOFIX-\\d\\d\\d:\\d\\d\\d\\$", ""
								+ (mvReleaser.isNfoFix() ? 'X' : ' '),
						"$NFOFIX-XXX:XXX$".length());

				if (nfoPS != null)
					nfoPS.println(line);

				result.append("\n" + line);
			}
		} while (line != null);

		return new String(result);
	}

	@Deprecated
	private String replaceField(String s, String stringPattern,
			String destination, int stringPatternLength) {

		// set a pattern for artistString
		Pattern pattern = Pattern.compile(stringPattern);
		// get a matcher for the string received s
		Matcher matcher = pattern.matcher(s);
		// replace
		// String replaced = matcher.replaceAll(destination);

		String replaced = StringUtilities.replacePosition(s, destination,
				matcher.start(), matcher.start() + stringPatternLength,
				stringPatternLength);
		return replaced;
	}

	private String replaceSpecialField(String s, String stringPattern,
			String destinationFull, int stringPatternLength) {

		String destination = destinationFull;
		
		if (s == null)
			System.out.println("line is null");
		
		// set a pattern for stringPattern
		Pattern pattern = Pattern.compile(stringPattern);
		// get a matcher for the string received s
		Matcher matcher = pattern.matcher(s);
		// retrieve the matches
		while (matcher.find()) {
			String matched = s.substring(matcher.start(), matcher.end());

			/*
			 * matched has the following form : $*-XXX:XXX$ where X are numbers
			 * and * is everything. The last XXX numbers indicates by how many
			 * caracters this field can be replaced. The first XXX numbers
			 * indicate how many caracters has been even replaced somewhere
			 * else.
			 */
			String s_fieldLength = matched.substring(matched.length() - 4,
					matched.length() - 1);
			String s_nbCaractersEvenReplaced = matched.substring(matched
					.length() - 8, matched.length() - 5);
			int fieldLength = Integer.parseInt(s_fieldLength);
			int nbCaractersEvenReplaced = Integer
					.parseInt(s_nbCaractersEvenReplaced);

			/*
			 * if destinationFull is null, then we just remove the pattern field
			 */
			if (destinationFull == null) {
				s = StringUtilities.replacePosition(s, "", matcher.start(),
						matcher.start() + fieldLength, stringPatternLength);
				continue;
			}

			/*
			 * if the nbCaractersEvenReplaced is larger or equal than the
			 * complete destination string, then the field has ever been filled
			 * somewhere else ! So just remove the current field and break the
			 * loop.
			 */
			if (nbCaractersEvenReplaced >= destination.length()) {
				s = StringUtilities.replacePosition(s, "", matcher.start(),
						matcher.start() + fieldLength, stringPatternLength);
				continue;
			}

			
			
			/*
			 * if nbCaractersEvenReplaced != 0 and destination.length() >
			 * nbCaractersEvenReplaced, it means we even begins to replace
			 * another field but there wasn't enough caracters allowed. So,
			 * let's continue the job !
			 */
			if (nbCaractersEvenReplaced != 0
					&& destination.length() > nbCaractersEvenReplaced)
				destination = destination.substring(nbCaractersEvenReplaced);

			if (destination.length() > fieldLength)
				destination = destination.substring(0, fieldLength);

			s = StringUtilities.replacePosition(s, destination,
					matcher.start(), matcher.start() + fieldLength,
					stringPatternLength);
		}
		return s;
	}
}
